﻿var manager = null;
var url = "../Permission/TreeMenuDate";
$(function () {
    menuInit();
    InitOrgData();
});
 function InitOrgData() {
     $.get(url, {}, function (orgdata) {
         if (orgdata != undefined) {
             if (manager != null && manager != undefined) { manager.clear(); }
             manager = $(".l-tree").ligerTree({
                 data: [orgdata],
                 checkbox: true,
                 onClick: itemclick
             });
             $("#initLoadingImg").css("display", "none");

         }
     });
 }
 function itemclick(item, i) {
     if (item != null && item != undefined && item.data != null && item.data != undefined) {
         $("#getnodeid").val(item.data.id);
         $("#getnodename").val(item.data.text);
         $("#getnodeisorg").val(item.data.isorg);
     }
 }


 function menuInit() {
     $("#toptoolbar").ligerToolBar({ items: [
                { text: '增加资源', click: addPermission, icon: 'add' },
                { line: true },
                 { text: '增加页面资源', click: addUiPermission, icon: 'add' },
                { line: true },
                { text: '修改', click: editOperation, icon: 'modify' },
                { line: true },
                { text: '删除', click: deleteOperation, img: '../Content/images/delete.gif' }
            ]
     });
 }
 //
 //add
 function addPermission() {
     $.ligerDialog.open({ url: '../Permission/Add', height: getDialogHeight(), width: getDialogWidth(),
      isResize: true, isHidden: false
     });
 }

 //add ui permission
 function addUiPermission() {
     var id = $("#getnodeid").val();
     var isorg = $("#getnodeisorg").val();
     if (id == undefined || id.toString().length <= 0) {
         $.ligerDialog.success('请选择父元素结点!'); return;
     }
     if (isorg == undefined || isorg.toString()!="1") {
         $.ligerDialog.success('子页面元素不能再添加!'); return;
     }
     if (isSysRoot()) {
         $.ligerDialog.success('不能编辑根结点!'); return;
     }
     $.ligerDialog.open({ url: '../Permission/AddUiPermisson?permissionId=' + id, height: getDialogHeight(), width: getDialogWidth(),
      isResize: true, isHidden: false
     });
 }
 //编辑菜单项
 function editOperation() {
     var id = $("#getnodeid").val();
     var isorg = $("#getnodeisorg").val();
     if (id == undefined || id.toString().length <= 0) {
         $.ligerDialog.success('请选择要修改的元素结点!'); return;
     }
     var editurl = "../Permission/Edit?id="+id;
     if (isorg == undefined || isorg.toString() != "1") {
         editurl = "../Permission/EditUiPermisson?id=" + id;
     }
     if (isSysRoot()) {
         $.ligerDialog.success('不能编辑根结点!'); return;
     }
     $.ligerDialog.open({ url: editurl, height: getDialogHeight(), width: getDialogWidth(), isResize: true, isHidden: false});
 }
 //删除菜单项
 function deleteOperation() {
     var id = $("#getnodeid").val();
     var isorg = $("#getnodeisorg").val();
     if (id == undefined || id.toString().length <= 0) {
         $.ligerDialog.success('请选择要删除的元素结点!'); return;
     }
     var deleteurl = "../Permission/DeletePermisson";
     if (isorg == undefined || isorg.toString() != "1") {
         deleteurl = "../Permission/DeleteUiPermisson";
     }
     if (isSysRoot()) {
         $.ligerDialog.success('不能删除根结点!'); return;
     }
     $.get(deleteurl, { "id": id }, function (data) {
         var tip = "删除失败！";
         if (data != undefined && data == "ok") {
             tip = "删除成功！";
             InitOrgData();
         }
         $.ligerDialog.success(tip); return;
     });
 }
 //判断是否是系统内置根结点
 function isSysRoot() {
     var id = $("#getnodeid").val();

     
     if(id==$("#syspermissionRoot").val())
     {
        return true;
     }
     return false;
 }